<?php
class My_Service_Soap
{
    protected $_client;
    
    public function timeRequest($dateStart = null, $dateEnd = null){
        if(isset($dateStart) && isset($dateEnd)
        && $dateStart && $dateEnd
        ){
            $result['dateStart'] = $dateStart;
            $result['dateEnd'] = $dateEnd;
            
            $result['dateYesterday'] = date("Y-m-d",strtotime($dateStart)-86400);
            $result['dateTomorrow'] = date("Y-m-d",strtotime($dateEnd)+86400);
            
            $result['dateNextStart'] = $result['dateTomorrow'];
            $result['dateNextEnd'] = date("Y-m-d",strtotime($dateEnd)+86400+(strtotime($dateEnd) - strtotime($dateStart)));
            
            $result['datePrivStart'] = date("Y-m-d",strtotime($dateStart)-86400-(strtotime($dateEnd) - strtotime($dateStart)));
            $result['datePrivEnd'] = $result['dateYesterday'];
        }else{
            $dateEnd = date("Y-m-d",time()-86400);
            $month = date("m",strtotime($dateEnd));
            $year = date("Y",strtotime($dateEnd));
            $dateStart = "{$year}-{$month}-01";
            $result = $this->timeRequest($dateStart,$dateEnd);
        }
        return $result;
    }

    public function getClient($config)
    {
        //check cache instance
        if($this->_client instanceof Zend_Soap_Client){
            return $this->_client;
        }

        //check input
        if( !isset($config->wsdl[0]) ||
            !isset($config->function[0])){
            return null;
        }

        //init service connection
        try{
            $this->_client = new Zend_Soap_Client($config->wsdl);
            return $this->_client;
        }catch (SoapFault $e) {
            //write log here
            //TODO
            return null;
        }catch(Excetion $ex){
            //write log here
            //TODO
            return null;
        }
    }
    // prepareParams API revenue, pcu, acu, active user, register user, convert user
    public function prepareParamsWidget($config)
    {
        if((isset($config['dateStart']) && $config['dateStart'] >= date("Y-m-d"))){
            $config['dateStart'] = date("Y-m-d",time()-86400);
        }
        if((isset($config['dateEnd']) && $config['dateEnd'] >= date("Y-m-d"))){
            $config['dateEnd'] = date("Y-m-d",time()-86400);
        }
        $secret = isset($config['secret']) ? $config['secret'] : '';
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId']) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey']) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['index']) ? $config['index'] : '';
        $parameters['argument3'] = isset($config['dateStart']) ? $config['dateStart'] : '';
        $parameters['argument4'] = isset($config['dateEnd']) ? $config['dateEnd'] : '';
        $parameters['argument5'] = isset($config['channel']) ? $config['channel'] : '';
        $parameters['argument6'] = isset($config['server']) ? $config['server'] : '';
        $parameters['argument7'] = md5($parameters['argument1'] . md5($parameters['argument2']) . $secret);

        return $parameters;
    }
    // prepareParams API revenue rea time
    public function prepareParamsWidgetRealtime($config)
    {
        if((isset($config['dateStart']) && $config['dateStart'] > date("Y-m-d"))){
            $config['dateStart'] = date("Y-m-d");
        }
        if((isset($config['dateEnd']) && $config['dateEnd'] > date("Y-m-d"))){
            $config['dateEnd'] = date("Y-m-d");
        }
        $secret = isset($config['secret']) ? $config['secret'] : '';
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId']) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey']) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['index']) ? $config['index'] : '';
        $parameters['argument3'] = isset($config['dateStart']) ? $config['dateStart'] : '';
        $parameters['argument4'] = isset($config['dateEnd']) ? $config['dateEnd'] : '';
        $parameters['argument5'] = isset($config['channel']) ? $config['channel'] : '';
        $parameters['argument6'] = isset($config['server']) ? $config['server'] : '';
        $parameters['argument7'] = md5($parameters['argument1'] . md5($parameters['argument2']) . $secret);

        return $parameters;
    }
    // prepareParams CCU
    public function prepareParamsCCU($config)
    {
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['dateStart'][0]) ? $config['dateStart'] : ''; 
        $parameters['argument3'] = isset($config['dateEnd'][0]) ? $config['dateEnd'] : ''; 
        $parameters['argument4'] = isset($config['server']) ? $config['server'] : '';
        $parameters['argument5'] = isset($config['channelingList']) ? $config['channelingList'] : '';
        $parameters['argument6'] = isset($config['serverChannelingList']) ? $config['serverChannelingList'] : '';
        $parameters['argument7'] = isset($config['recordNumber']) ? $config['recordNumber'] : '';
        $secret = isset($config['secret']) ? $config['secret'] : '';
        $signature = md5($parameters['argument1'] . md5($parameters['argument2']) . $secret);
        $parameters['argument8'] = $signature;

        return $parameters;
    }
    // prepareParams transaction game trong nhà
    public function prepareParamsTransaction($config)
    {
        if((isset($config['dateStart']) && $config['dateStart'] > date("Y-m-d"))){
            $config['dateStart'] = date("Y-m-d");
        }
        if((isset($config['dateEnd']) && $config['dateEnd'] > date("Y-m-d"))){
            $config['dateEnd'] = date("Y-m-d");
        }
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['dateStart'][0]) ? $config['dateStart'] : '';
        $parameters['argument3'] = isset($config['dateEnd'][0]) ? $config['dateEnd'] : '';
        $parameters['argument4'] = isset($config['username'][0]) ? $config['username'] : '';
        $parameters['argument5'] = isset($config['serverId'][0]) ? $config['serverId'] : '';
        $parameters['argument6'] = isset($config['status']) ? $config['status'] : '';
        $parameters['argument7'] = isset($config['channelingAgentId'][0]) ? $config['channelingAgentId'] : '';
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';                
        $parameters['argument8'] = md5($parameters['argument1'] . md5($parameters['argument2'] . $parameters['argument3']) . $secret);

        return $parameters;
    }
    // prepareParams transaction game tích hợp
    public function prepareParamsTransactionGP0($config)
    {
        if((isset($config['dateStart']) && $config['dateStart'] > date("Y-m-d"))){
            $config['dateStart'] = date("Y-m-d");
        }
        if((isset($config['dateEnd']) && $config['dateEnd'] > date("Y-m-d"))){
            $config['dateEnd'] = date("Y-m-d");
        }
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['dateStart'][0]) ? $config['dateStart'] : '';
        $parameters['argument3'] = isset($config['dateEnd'][0]) ? $config['dateEnd'] : '';
        $parameters['argument4'] = isset($config['username'][0]) ? $config['username'] : '';
        $parameters['argument5'] = isset($config['serverId'][0]) ? $config['serverId'] : '';
        $parameters['argument6'] = isset($config['status']) ? $config['status'] : '';
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';                
        $parameters['argument7'] = md5($parameters['argument1'] . md5($parameters['argument2'] . $parameters['argument3']) . $secret);
        return $parameters;
    }
    // prepareParams card transaction game mobile
    public function prepareParamsCardTransaction($config)
    {
        if((isset($config['dateStart']) && $config['dateStart'] > date("Y-m-d"))){
            $config['dateStart'] = date("Y-m-d");
        }
        if((isset($config['dateEnd']) && $config['dateEnd'] > date("Y-m-d"))){
            $config['dateEnd'] = date("Y-m-d");
        }
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['index'][0]) ? $config['index'] : '';
        $parameters['argument3'] = isset($config['dateStart'][0]) ? $config['dateStart'] : '';
        $parameters['argument4'] = isset($config['dateEnd'][0]) ? $config['dateEnd'] : '';
        $parameters['argument5'] = isset($config['username'][0]) ? $config['username'] : '';
        $parameters['argument6'] = isset($config['status'][0]) ? $config['status'] : '';
        $parameters['argument7'] = isset($config['channelingAgentId'][0]) ? $config['channelingAgentId'] : '';
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';                
        $parameters['argument8'] = md5($parameters['argument1'] . md5($parameters['argument2'] . $parameters['argument3'] . $parameters['argument4'] . $parameters['argument5'] . $parameters['argument6'] . $parameters['argument7']) . $secret);

        return $parameters;
    }
    // prepareParams shopping
    public function prepareParamsShop($config)
    {
        if((isset($config['dateStart']) && $config['dateStart'] >= date("Y-m-d"))){
            $config['dateStart'] = date("Y-m-d",time()-86400);
        }
        if((isset($config['dateEnd']) && $config['dateEnd'] >= date("Y-m-d"))){
            $config['dateEnd'] = date("Y-m-d",time()-86400);
        }
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['index'][0]) ? $config['index'] : '';
        $parameters['argument3'] = isset($config['dateStart']) ? $config['dateStart'] : '';
        $parameters['argument4'] = isset($config['dateEnd']) ? $config['dateEnd'] : '';
        $parameters['argument5'] = isset($config['itemId']) ? $config['itemId'] : '';
        $parameters['argument6'] = isset($config['username']) ? $config['username'] : '';
        $parameters['argument7'] = isset($config['serverId']) ? $config['serverId'] : '';
        $parameters['argument8'] = isset($config['sortCondition']) ? $config['sortCondition'] : 'time';
        $parameters['argument9'] = isset($config['sortType']) ? $config['sortType'] : 'DESC';
        
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';
        $signature = md5($parameters['argument1'] . md5($parameters['argument2']) . $secret);
        $parameters['argument10'] = $signature;

        return $parameters;
    }
    // prepareParams expense
    public function prepareParamsExpense($config)
    {
        if((isset($config['dateStart']) && $config['dateStart'] >= date("Y-m-d"))){
            $config['dateStart'] = date("Y-m-d",time()-86400);
        }
        if((isset($config['dateEnd']) && $config['dateEnd'] >= date("Y-m-d"))){
            $config['dateEnd'] = date("Y-m-d",time()-86400);
        }
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['index'][0]) ? $config['index'] : '';
        $parameters['argument3'] = isset($config['dateStart']) ? $config['date'] : '';
        $parameters['argument4'] = isset($config['dateEnd']) ? $config['dateEnd'] : '';
        $parameters['argument5'] = isset($config['serverId']) ? $config['serverId'] : '';
        $parameters['argument6'] = isset($config['sortCondition']) ? $config['sortCondition'] : 'server_id';
        $parameters['argument7'] = isset($config['sortType']) ? $config['sortType'] : 'DESC';
        
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';
        $signature = md5($parameters['argument1'] . md5($parameters['argument2']) . $secret);
        $parameters['argument8'] = $signature;

        return $parameters;
    }
    // prepareParams exchange
    public function prepareParamsExchange($config)
    {
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['dateStart'][0]) ? $config['dateStart'] : '';
        $parameters['argument3'] = isset($config['dateEnd'][0]) ? $config['dateEnd'] : '';
        $parameters['argument4'] = isset($config['channelingAgentId'][0]) ? $config['channelingAgentId'] : '';
        $parameters['argument5'] = isset($config['serverId'][0]) ? $config['serverId'] : '';
        $parameters['argument6'] = isset($config['minGold']) ? $config['minGold'] : '';
        $parameters['argument7'] = isset($config['maxGold']) ? $config['maxGold'] : '';
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';                
        $signature = md5($parameters['argument1'] . md5($parameters['argument2'] . $parameters['argument3']) . $secret);
        $parameters['argument8'] = $signature;

        return $parameters;
    }
    // prepareParams kpi exchange
    public function prepareParamsKPIExchange($config)
    {
        $parameters = array();
        $parameters['argument0'] = isset($config['agentId'][0]) ? $config['agentId'] : '';
        $parameters['argument1'] = isset($config['agentKey'][0]) ? $config['agentKey'] : '';
        $parameters['argument2'] = isset($config['dateStart'][0]) ? $config['dateStart'] : '';
        $parameters['argument3'] = isset($config['dateEnd'][0]) ? $config['dateEnd'] : '';
        $parameters['argument4'] = isset($config['channelingAgentId'][0]) ? $config['channelingAgentId'] : '';
        $parameters['argument5'] = isset($config['serverId'][0]) ? $config['serverId'] : '';
        $parameters['argument6'] = isset($config['periodGold']) ? $config['periodGold'] : '';
        $secret = isset($config['secret'][0]) ? $config['secret'] : '';
        $signature = md5($parameters['argument1'] . md5($parameters['argument2'] . $parameters['argument3'] . $parameters['argument4'] . $parameters['argument5'] . $parameters['argument6']) . $secret);
        $parameters['argument7'] = $signature;

        return $parameters;
    }
    // lay data từ service
    public function runService($option){
        try{
            if(!$option['service'] || !$option['function'] || !$option['params']){
                return;
            }
            $myMem = new My_Mem();
            $key = 'KPIReport-'.md5($option['function'].md5(json_encode($option['params'])));
            
            # khong cache
            if($myMem->get($key)){
                $result = $myMem->get($key);
            }else{
                $result = $option['service']->{$option['function']}($option['params']);
                if(!isset($option['nocache'])){
                    $myMem->set($key,$result);
                }
            }
        }catch(exception $ex){
            //echo $ex->getMessage();
            return array('status'=>-1,'result'=>'Có lỗi sảy ra trong quá trình lấy dữ liệu. Vui lòng thử lại khi khác hoặc báo cho người quản lý tool ('+$ex->getCode()+')');
        }
        return $result;
    }
}